/*
 * Created on 10.09.2007
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package at.fhv.dcs.aw.j2me.lcdui.basics;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class LCDUIBasicsMIDlet extends MIDlet implements CommandListener, ItemStateListener  {

    private final Command m_exitCmd;

    private Form m_form;
    private Alert m_doYouReallyWantToExitAlert;
    private TextField m_textField;
    private Alert m_alert;

    public LCDUIBasicsMIDlet() {
        m_exitCmd = new Command("Exit", Command.EXIT, 1);

        m_form = new Form("Just a test");
        Display.getDisplay(this).setCurrent(m_form);

        m_form.addCommand(m_exitCmd);
        m_form.setCommandListener(this);
        m_form.setItemStateListener(this);

        m_textField = new TextField("Text", null, 10, TextField.ANY);

        m_form.append(m_textField);

        m_alert = new Alert("Wrong input");
        m_alert.setTimeout(Alert.FOREVER);
    }

    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub
    }

    protected void startApp() throws MIDletStateChangeException {
        // TODO Auto-generated method stub
    }

    public void commandAction(Command cmd, Displayable d) {

        if (cmd == m_exitCmd) {

            /* Version 1:this will cause the an exit of the application immediately */
//            m_doYouReallyWantToExitAlert = new Alert(null, "Do you really want to exit",null, AlertType.CONFIRMATION);
//            m_doYouReallyWantToExitAlert.setTimeout(Alert.FOREVER);
//            Display.getDisplay(this).setCurrent(m_doYouReallyWantToExitAlert);
//
//            try {
//                destroyApp(true);
//            } catch (MIDletStateChangeException e) {
//                e.printStackTrace();
//            }
//            notifyDestroyed();
            /* end of version 1*/

            /* Version 2 */
            m_doYouReallyWantToExitAlert = new Alert(null, "Do you really want to exit",null, AlertType.ERROR);
            m_doYouReallyWantToExitAlert.setTimeout(Alert.FOREVER);
            m_doYouReallyWantToExitAlert.setCommandListener(this);
            Display.getDisplay(this).setCurrent(m_doYouReallyWantToExitAlert);
            /* end of version 2 */

        } else if (cmd == Alert.DISMISS_COMMAND && d == m_doYouReallyWantToExitAlert) {
            try {
                destroyApp(true);
            } catch (MIDletStateChangeException e) {
                e.printStackTrace();
            }
            notifyDestroyed();
        }

    }

    public void itemStateChanged(Item item) {
        if (item == m_textField) {
            String value = m_textField.getString();
            for (int i = 0; i < value.length(); i++) {
                if (value.charAt(i) < 'a' || value.charAt(i) > 'z') {
                    Display.getDisplay(this).setCurrent(m_alert);
                    break;
                }
            }
        }
    }
}
